using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Entitas;

/// <summary>
/// 游戏面板的响应系统
/// </summary>
public class GameBoardSystem : ReactiveSystem<GameEntity>, IInitializeSystem
{
    private IGroup<GameEntity> _itemsGroup;

    public GameBoardSystem(Contexts context) : base(context.game)
    {
        _itemsGroup = context.game.GetGroup(GameMatcher.GameBoardItem);
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.GameBoard);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasGameBoard;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        var gameBoard = entities.SingleEntity().gameBoard;
        foreach (var entity in entities)
        {
        }
    }

    public void Initialize()
    {
        var gameBoard = CreaterService.Instance.CreateGameBoard().gameBoard;
        CustomVector2 index = new CustomVector2();
        for (int x = 0; x < gameBoard.columns; x++)
        {
            for (int y = 0; y < gameBoard.rows; y++)
            {
                index.x = x;
                index.y = y;
                if (RandomBlocker())
                {
                    CreaterService.Instance.CreateBlock(index);
                }
                else
                {
                    CreaterService.Instance.CreateBall(index);
                }
            }
        }
    }

    private bool RandomBlocker() => Random.Range(0, 10) < 1;
}